package com.example.weblending.controlller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.weblending.domain.ApplyNum;
import com.example.weblending.domain.ApplyUnset;
import com.example.weblending.domain.User;
import com.example.weblending.exception.GlobalException;
import com.example.weblending.service.ApplyNumService;
import com.example.weblending.service.ApplyUnsetService;
import com.example.weblending.service.UserService;
import com.example.weblending.utils.JwtUtils;
import com.example.weblending.utils.MailUtil;
import com.example.weblending.utils.Result;
import com.example.weblending.vo.UnsetPassword;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

@RestController
@RequestMapping("/apply")
@CrossOrigin
public class ApplyController {
    @Resource
    private UserService userService;
    @Resource
    private ApplyUnsetService applyUnsetService;
    @Resource
    private ApplyNumService applyNumService;

    /**
     * 新建重置密码申请
     */
    @PostMapping("password/unset")
    public Result applayUnset(@RequestBody UnsetPassword apply){
        User user=userService.findOneByName(apply.getName());
        if(user==null){
            throw new GlobalException(404,"未找到该用户");
        }
        ApplyUnset applyUnset=new ApplyUnset();
        applyUnset.setUid(user.getUid());
        applyUnset.setCertificate(apply.getCertificate());
        applyUnset.setPremission(user.getPermission());
        boolean success=applyUnsetService.apply(applyUnset);
        return success?Result.ok():Result.error();
    }

    /**
     * 管理员获得待处理的重置密码申请列表
     */
    @GetMapping("password/list/{page}/{limit}")
    public Result list(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<ApplyUnset> userpage =applyUnsetService.getListyId(page,limit,user);
        return Result.ok().data(userpage).data("item",userpage.getRecords());
    }

    /**
     * 管理员获得自己已经处理完成的申请列表
     */
    @GetMapping("password/history/{page}/{limit}")
    public Result history(@PathVariable long page, @PathVariable long limit, HttpServletRequest request){
        User user=JwtUtils.isOperator(request,userService);
        Page<ApplyUnset> userpage =applyUnsetService.getHistoryId(page,limit,user);
        return Result.ok().data(userpage).data("item",userpage.getRecords());
    }

    /**
     * 管理员同意重置密码申请，并通过邮箱向用户发送 一次性登陆码
     */
    @PostMapping("password/{uid}/reset")
    public Result resetPassword(@PathVariable String uid, HttpServletRequest request){
        User operator=JwtUtils.isOperator(request,userService);
        ApplyUnset applyUnset=applyUnsetService.getById(uid);
        boolean success=applyUnsetService.reset(applyUnset,operator);
        return success?Result.ok().data("code",applyUnset.getCode()):Result.error();
    }

}
